package cn.uce.cc.srv.biz.impl;

import cn.uce.base.entity.CcRobotRecord;
import cn.uce.base.vo.CcRobotRecordVo;
import cn.uce.cc.srv.service.CcRobotRecordService;
import cn.uce.srp.api.service.biz.ICcRobotRecordBiz;
import cn.uce.utils.StringUtil;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
   * @Author liupan
   * @Description 
   * @Date  2020-06-04 16:20
   */
@Service
@Transactional(readOnly = true, propagation= Propagation.SUPPORTS, rollbackFor = Exception.class)
public class CcRobotRecordBiz  implements ICcRobotRecordBiz
{
    private static final Logger log = LoggerFactory.getLogger(CcRobotRecordBiz.class);


    @Resource
    private CcRobotRecordService ccRobotRecordService;

    /**
     * 提供给科大讯飞的创建接口经过uop调用
     * @param ccRobotRecord
     * @return
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Map<String, String> createCcRobotRecord(CcRobotRecord ccRobotRecord) {
        Map<String, String> resultMap = new HashMap<>();
        Integer ccRobotRecord1 = 0;
        if (ccRobotRecord != null && StringUtil.isNotBlank(ccRobotRecord.getCallId())){
            try {
                ccRobotRecord1 = ccRobotRecordService.createCcRobotRecord(ccRobotRecord);
                log.warn("createCcRobotRecord"+JSON.toJSONString(ccRobotRecord)+"return"+ccRobotRecord1);
                resultMap.put("isSuccess","T");
                resultMap.put("errorMsg","成功");
            } catch (Exception e) {
                resultMap.put("isSuccess","F");
                resultMap.put("errorMsg","出现异常");
                log.warn("createCcRobotRecord"+JSON.toJSONString(ccRobotRecord),e);
            }
        }
        return resultMap;
    }


    public static void main(String[] args) {
        CcRobotRecord cs = new CcRobotRecord();
        cs.setCallId("123");
        cs.setVoiceId("123");
        cs.setVoiceAddress("123");
        cs.setCallPhoneNum("13812341234");
        cs.setCallArea("上海市");
        cs.setBillCode("51812341234");
        Date date = new Date();
        cs.setBeginTime(date);
        cs.setEndTime(date);
        cs.setNormalEnd(false);
        cs.setEndType(1);
        cs.setRequirementNum(1);
        cs.setCallIntentionList("1");
        cs.setTransferSeatGroup("");
        cs.setOneHourRepeat(false);
        cs.setCommunicationContent("1");
        cs.setCreateTime(date);
        System.out.println(JSON.toJSONString(cs));
    }

    /**
     * cc界面查询方法
     * @param ccRobotRecordVo
     * @return
     */
    @Override
    public List<CcRobotRecordVo> findCcRoBotRecord(CcRobotRecordVo ccRobotRecordVo) {
        List<CcRobotRecordVo> returnList =  ccRobotRecordService.findCcRoBotRecord(ccRobotRecordVo);
        updateFields(returnList);
        log.warn("createCcRobotRecord-"+JSON.toJSONString(ccRobotRecordVo)+"return:"+returnList);
        return returnList;
    }

    /**
     * 对list的字段进行设值
     * @param returnList
     */
    private void updateFields(List<CcRobotRecordVo> returnList) {
        if (returnList != null && !returnList.isEmpty()){
            for (CcRobotRecordVo ccRobotRecordVo : returnList) {
                Integer endType = ccRobotRecordVo.getEndType();
//                1转人工、2转满意度、3转IVR、4挂机
                if ( endType != null && endType ==1){
                    ccRobotRecordVo.setEndTypeStr("转人工");
                }else if (endType != null && endType ==2){
                    ccRobotRecordVo.setEndTypeStr("转满意度");
                }else if (endType != null && endType ==3){
                    ccRobotRecordVo.setEndTypeStr("转IVR");
                }else if (endType != null && endType ==4){
                    ccRobotRecordVo.setEndTypeStr("挂机");
                }else{
                    ccRobotRecordVo.setEndTypeStr(String.valueOf(ccRobotRecordVo.getEndType()));
                }
                Boolean oneHourRepeat = ccRobotRecordVo.getOneHourRepeat();
                if (oneHourRepeat != null){
                    ccRobotRecordVo.setOneHourRepeatStr(oneHourRepeat?"重复":"不重复");
                }
                Boolean normalEnd=  ccRobotRecordVo.getNormalEnd();
                if (normalEnd != null){
                    ccRobotRecordVo.setNormalEndStr(oneHourRepeat?"正常结束":"非正常结束");
                }
                String transferSeatGroup = ccRobotRecordVo.getTransferSeatGroup();
                if (StringUtil.isNotBlank(transferSeatGroup) && "0".equals(transferSeatGroup)){
                    ccRobotRecordVo.setTransferSeatGroup("挂断");
                }else if (StringUtil.isNotBlank(transferSeatGroup) && "1".equals(transferSeatGroup)){
                    ccRobotRecordVo.setTransferSeatGroup("转人工");
                }
            }
        }
    }


}
